package com.cskaoyan.service;

import com.cskaoyan.bean.catalog.CatalogVo;
import com.cskaoyan.bean.category.Category;
import com.cskaoyan.bean.category.CategoryExample;
import com.cskaoyan.mapper.CategoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CatalogServiceImpl implements CatalogService {
    @Autowired
    CategoryMapper categoryMapper;
    @Override
    public CatalogVo catalogList() {
        CatalogVo catalogVo = new CatalogVo();

        CategoryExample example = new CategoryExample();
        CategoryExample.Criteria criteria = example.createCriteria();
        criteria.andLevelEqualTo("L1");
        criteria.andDeletedEqualTo(false);
        List<Category> categoryList = categoryMapper.selectByExample(example);
        catalogVo.setCategoryList(categoryList);

        example.setOrderByClause("id limit 1");
        List<Category> currentCategory = categoryMapper.selectByExample(example);
        catalogVo.setCurrentCategory(currentCategory.get(0));

        CategoryExample exampleL2 = new CategoryExample();
        CategoryExample.Criteria criteriaL2 = exampleL2.createCriteria();
        criteriaL2.andLevelEqualTo("L2");
        criteriaL2.andPidEqualTo(currentCategory.get(0).getId());
        criteriaL2.andDeletedEqualTo(false);
        List<Category> currentSubCategory = categoryMapper.selectByExample(exampleL2);
        catalogVo.setCurrentSubCategory(currentSubCategory);
        return catalogVo;
    }

    @Override
    public CatalogVo currentCatalog(Integer id) {
        CatalogVo catalogVo = new CatalogVo();
        Category currentCategory = categoryMapper.selectByPrimaryKey(id);
        catalogVo.setCurrentCategory(currentCategory);


        CategoryExample exampleL2 = new CategoryExample();
        CategoryExample.Criteria criteriaL2 = exampleL2.createCriteria();
        criteriaL2.andLevelEqualTo("L2");
        criteriaL2.andPidEqualTo(id);
        criteriaL2.andDeletedEqualTo(false);
        List<Category> currentSubCategory = categoryMapper.selectByExample(exampleL2);
        catalogVo.setCurrentSubCategory(currentSubCategory);
        return catalogVo;
    }
}
